package com.normal.zbase.impl.tools.obj

import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.normal.zbase.R

/**
 * 三疯
 * *************************
 *
 * *************************
 */
class ToolbarFactory private constructor() {
    companion object {
        fun build(toolbar: Toolbar, activity: AppCompatActivity): Builder {//静态实例化建造者Builder类
            return Builder(toolbar, activity)
        }
    }

    class Builder constructor(var toolbar: Toolbar, var activity: AppCompatActivity) {
        init {
            activity.setSupportActionBar(toolbar)
            activity.supportActionBar?.setDisplayShowTitleEnabled(false)
        }

        fun setBack(callback: (() -> Unit) = {},isFinish:Boolean = true, icBack: Int = R.drawable.ic_back): Builder {
            activity.supportActionBar?.setDisplayHomeAsUpEnabled(true)
            activity.supportActionBar?.setHomeAsUpIndicator(icBack)
            toolbar.setNavigationOnClickListener {
                if (isFinish) {
                    activity.finish()
                } else {
                    callback.invoke()
                }
            }
            return this
        }

        fun setBackgroundColor(colors: Int): Builder {
            toolbar.setBackgroundColor(colors)
            return this
        }

        fun setTitle(textView: TextView,str:String):Builder{
            textView.text = str
            return this
        }
    }
}